package com.mktech.taraservice.db;

import java.util.Iterator;
import java.util.Set;

import com.mktech.taraservice.db.base.BaseDBImpl;
import com.mktech.taraservice.util.LogTool;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.net.Uri;

public class DBHelper extends SQLiteOpenHelper
{
	private final String TAG = DBHelper.class.getSimpleName();
	private static final String DATABASE_NAME = "taraContent.db";
	private static final int DATABASE_VERSION = 1;
	private static DBHelper mInstance;

	public static DBHelper obtain(Context context)
	{
		if (mInstance == null)
		{
			mInstance = new DBHelper(context);
		}
		return mInstance;
	}

	private DBHelper(Context context)
	{
		super(context, DATABASE_NAME, null, DATABASE_VERSION);
		// TODO Auto-generated constructor stub
	}

	@Override
	public void onCreate(SQLiteDatabase db)
	{
		// TODO Auto-generated method stub
		createTable(db, MovieDBManager.TABLE_NAME, MovieDBManager.movieAttrs);
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
	{
		// TODO Auto-generated method stub
		dropTable(db, MovieDBManager.TABLE_NAME);
		onCreate(db);
	}

	private boolean createTable(SQLiteDatabase db, String tableName, TableAttrs attrs)
	{
		if (attrs == null)
		{
			return false;
		}
		String sql;
		Set<String> keySet = attrs.getKeySet();
		if (keySet.size() == 0)
		{
			return false;
		}
		StringBuilder builder = new StringBuilder();
		builder.append("create table if not exists ");
		builder.append(tableName);
		builder.append(" ( ");
		for (Iterator<String> iterator = keySet.iterator(); iterator.hasNext();)
		{
			String key = iterator.next();
			builder.append(" " + key + " ");
			builder.append(attrs.getValue(key));
			builder.append(",");
		}
		builder.deleteCharAt(builder.length() - 1);
		builder.append(")");

		LogTool.i("builder = " + builder.toString());

		try
		{
			sql = builder.toString();
			db.execSQL(sql);
			return true;
		}
		catch (Exception e)
		{
			e.printStackTrace();
			return false;
		}
	}

	private boolean dropTable(SQLiteDatabase db, String tableName)
	{
		try
		{
			String sql = "drop table if exist " + tableName;
			db.execSQL(sql);
			return true;
		}
		catch (Exception e)
		{
			e.printStackTrace();
			return false;
		}
	}

	public int delete(String tableName, String selection, String[] selectionArgs)
	{
		// TODO Auto-generated method stub
		SQLiteDatabase db = getWritableDatabase();
		return db.delete(tableName, selection, selectionArgs);
	}

	public long insert(String tableName, ContentValues values)
	{
		// TODO Auto-generated method stub
		SQLiteDatabase db = getWritableDatabase();
		return db.insert(tableName, null, values);
	}

	public Cursor query(String tableName, String[] projection, String selection, String[] selectionArgs, String sortOrder)
	{
		// TODO Auto-generated method stub
		SQLiteDatabase db = getWritableDatabase();
		return db.query(tableName, null, selection, selectionArgs, null, null, sortOrder);
	}

	public int update(String tableName, ContentValues values, String selection, String[] selectionArgs)
	{
		// TODO Auto-generated method stub
		SQLiteDatabase db = getWritableDatabase();
		return db.update(tableName, values, selection, selectionArgs);
	}

}
